package com.example.xViewChat.NettyClient;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.example.xViewChat.Bean.IntentBean;
import com.example.xViewChat.R;
import com.example.xViewChat.Service.PushService;
import io.ganguo.chat.core.transport.IMRequest;

/**
 * Created by hasee on 2016-04-01.
 */
public class EventBroadcastReceiver extends BroadcastReceiver implements EventListener {
    public Context context;
    private NotificationManager notificationManager;

    @Override
    public void onReceive(Context ctx, Intent it) {
        System.out.println("it.getAction()=" + it.getAction());
        context = ctx;

        if (it.getAction().equals(Intent.ACTION_USER_PRESENT)
                || it.getAction().equals(Intent.ACTION_POWER_CONNECTED)
                || it.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)
                ) {
            startPushService();
        }

        if (it.getAction().equals(ConnectorManager.ACTION_NETWORK_CHANGED)) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            android.net.NetworkInfo info = connectivityManager.getActiveNetworkInfo();
            onDevicesNetworkChanged(info);
        }

        if (it.getAction().equals(ConnectorManager.ACTION_CONNECTION_CLOSED)) {

            System.out.println("In ConnectorManager.ACTION_CONNECTION_CLOSED ,```````````################");
            if (ConnectorManager.netWorkAvailable(context)) {
                PushManager.init(context);
            }
            onConnectionClosed();
        }

        if (it.getAction().equals(ConnectorManager.ACTION_CONNECTION_SUCCESS)) {

            System.out.println("In ConnectorManager.ACTION_CONNECTION_SUCCESS ,```````````################");
            onConnectionSucceed();
        }

        if (it.getAction().equals(ConnectorManager.ACTION_REPLY_RECEIVED)) {

            System.out.println("In ConnectorManager.ACTION_REPLY_RECEIVED ,```````````################");
            onReplyReceived((IntentBean) it.getSerializableExtra("intentBean"));
        }

        if (it.getAction().equals(ConnectorManager.ACTION_MESSAGE_RECEIVED)) {

            System.out.println("In ConnectorManager.ACTION_MESSAGE_RECEIVED ,```````````################");
            onMessageReceived((IntentBean) it.getSerializableExtra("intentBean"));
        }
    }

    private void onDevicesNetworkChanged(NetworkInfo info) {

        if (info != null) {
            PushManager.init(context);
        }

        onNetworkChanged(info);
    }

    private void startPushService() {
        Intent intent = new Intent(context, PushService.class);
        intent.putExtra(PushManager.SERVICE_ACTION, PushManager.ACTION_ACTIVATE_PUSH_SERVICE);
        context.startService(intent);
    }

    private void showNotify(Context context, String msg) {

        this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        String title = "ϵͳ��Ϣ";

        Notification notification = new Notification(R.drawable.icon_notify, title, System.currentTimeMillis());
        notification.defaults = Notification.DEFAULT_LIGHTS;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        Intent notificationIntent = new Intent(context, Activity.class);//TODO:ûд��
        PendingIntent contentIntent = PendingIntent.getActivity(context, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.contentIntent = contentIntent;
        notification.setLatestEventInfo(context, title, msg, contentIntent);
        notificationManager.notify(R.drawable.icon_notify, notification);

    }


    @Override
    public void onMessageReceived(IntentBean intentBean) {
        for (int index = 0; index < EventListenerManager.getListeners().size(); index++) {
            EventListenerManager.getListeners().get(index).onMessageReceived(intentBean);
        }
    }

    @Override
    public void onReplyReceived(IntentBean intentBean) {
        for (int index = 0; index < EventListenerManager.getListeners().size(); index++) {
            EventListenerManager.getListeners().get(index).onReplyReceived(intentBean);
        }
    }

    @Override
    public void onNetworkChanged(NetworkInfo networkinfo) {
        for (int index = 0; index < EventListenerManager.getListeners().size(); index++) {
            EventListenerManager.getListeners().get(index).onNetworkChanged(networkinfo);
        }
    }

    @Override
    public void onConnectionStatus(boolean isConnected) {
        for (int index = 0; index < EventListenerManager.getListeners().size(); index++) {
            EventListenerManager.getListeners().get(index).onConnectionStatus(isConnected);
        }
    }

    @Override
    public void onConnectionSucceed() {
        for (int index = 0; index < EventListenerManager.getListeners().size(); index++) {
            EventListenerManager.getListeners().get(index).onConnectionSucceed();
        }
    }

    @Override
    public void onConnectionClosed() {
        for (int index = 0; index < EventListenerManager.getListeners().size(); index++) {
            EventListenerManager.getListeners().get(index).onConnectionClosed();
        }
    }

    @Override
    public void onReLogin() {
        for (int index = 0; index < EventListenerManager.getListeners().size(); index++) {
            EventListenerManager.getListeners().get(index).onReLogin();
        }
    }

    @Override
    public void onLoginSuccess() {

    }

    @Override
    public void onLoginFailed() {

    }
}
